package GUIs;

import Configs.Config;
import Controllers.DangNhapController;
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

public class f_dangnhap extends javax.swing.JFrame {

    private static f_dangnhap _instance;
    private final DangNhapController dncontrol;
    
    private final Config conn;
    public f_dangnhap(Config conn) {
        this.conn = conn;     
        this.dncontrol = new DangNhapController(this.conn);
        initComponents();
        //
        ImageIcon icon = new ImageIcon(getClass().getResource("../img/house.png"));
        Image image = icon.getImage();
        setIconImage(image);
    }

    public static f_dangnhap getInstance(Config conn) {
        if (_instance == null) {
            _instance = new f_dangnhap(conn);
        }
        return _instance;
    }

    public void createAndShowUI() {
        setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnchinh = new javax.swing.JPanel();
        lbtaikhoan = new javax.swing.JLabel();
        lbmatkhau = new javax.swing.JLabel();
        tftaikhoan = new javax.swing.JTextField();
        tfmatkhau = new javax.swing.JPasswordField();
        btndangnhap = new javax.swing.JButton();
        btnthoat = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
        setUndecorated(true);
        setResizable(false);
        setType(java.awt.Window.Type.POPUP);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        lbtaikhoan.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbtaikhoan.setText("Tài khoản:");

        lbmatkhau.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbmatkhau.setText("Mật khẩu:");

        tfmatkhau.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfmatkhauKeyPressed(evt);
            }
        });

        btndangnhap.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btndangnhap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/32x32/icon_login.png"))); // NOI18N
        btndangnhap.setText("Đăng nhập");
        btndangnhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndangnhapActionPerformed(evt);
            }
        });
        btndangnhap.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btndangnhapKeyPressed(evt);
            }
        });

        btnthoat.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnthoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/32x32/exit.png"))); // NOI18N
        btnthoat.setText("Thoát");
        btnthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthoatActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/login.png"))); // NOI18N

        javax.swing.GroupLayout pnchinhLayout = new javax.swing.GroupLayout(pnchinh);
        pnchinh.setLayout(pnchinhLayout);
        pnchinhLayout.setHorizontalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnchinhLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbtaikhoan, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lbmatkhau, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tfmatkhau, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tftaikhoan, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnchinhLayout.createSequentialGroup()
                        .addComponent(btndangnhap)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnthoat, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(67, 67, 67)))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        pnchinhLayout.setVerticalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnchinhLayout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tftaikhoan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbtaikhoan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfmatkhau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbmatkhau))
                        .addGap(53, 53, 53)))
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnthoat, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btndangnhap, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnchinh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnchinh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.conn.closeConn();
    }//GEN-LAST:event_formWindowClosing

    private void btnthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthoatActionPerformed
        this.conn.closeConn();
        this.dispose();
    }//GEN-LAST:event_btnthoatActionPerformed

    private void btndangnhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btndangnhapActionPerformed
        this.dncontrol.ValidnVerify(this, tftaikhoan, tfmatkhau);
    }//GEN-LAST:event_btndangnhapActionPerformed

    private void btndangnhapKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btndangnhapKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        this.dncontrol.ValidnVerify(this, tftaikhoan, tfmatkhau);
        }
    }//GEN-LAST:event_btndangnhapKeyPressed

    private void tfmatkhauKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfmatkhauKeyPressed
       if (evt.getKeyCode() == KeyEvent.VK_ENTER) { 
           this.dncontrol.ValidnVerify(this, tftaikhoan, tfmatkhau);
       }
    }//GEN-LAST:event_tfmatkhauKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btndangnhap;
    private javax.swing.JButton btnthoat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lbmatkhau;
    private javax.swing.JLabel lbtaikhoan;
    private javax.swing.JPanel pnchinh;
    private javax.swing.JPasswordField tfmatkhau;
    private javax.swing.JTextField tftaikhoan;
    // End of variables declaration//GEN-END:variables
}
